package com.iflytek.uoamp.notify.core.action;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.iflytek.uoamp.common.notify.constant.ConstField;
import com.iflytek.uoamp.common.notify.enumerate.ContentType;
import com.iflytek.uoamp.common.notify.enumerate.State;
import com.iflytek.uoamp.common.notify.enumerate.Type;
import com.iflytek.uoamp.common.notify.obj.Notification;
import com.iflytek.uoamp.common.notify.util.LogInfo;
import com.iflytek.uoamp.common.notify.util.NotifyUtils;

public abstract class AbstractNotifyAction {
	
    protected LogInfo logInfo = null;
    protected static Map<String, Long> timeMaps = new HashMap<String, Long>();
    protected String uniqueID = "";

    private static final String FOCUSCONTACTFLAG = "focus_contact_flag";
    public abstract boolean doTask(Notification item);
    
    /**
     * 检查告警对象中的告警类型是否合法以及告警的状态值是否合法
     * @param notification
     * @return 二者均合法返回true,反之返回false
     */
    protected boolean integrityCheck(Notification notification) {
        State state = State.getState(notification.getState());
        if(state == State.ILLEGAL) {
            logInfo.addError(MessageFormat.format(ConstField.ILLEGALSTATE, state));
            return false;
        }
        
        Type type = Type.getType(notification.getType());
        if(type != Type.SEND_ONLY && StringUtils.isBlank(notification.getPackageName())) {
            logInfo.addError(ConstField.NULLUNIQUEKEY);
            return false;
        }
        
    	ContentType contentType = ContentType.getType(notification.getContentType());
        if((type == Type.FULL_TEMPLATE || type == Type.ID_ONLY) && contentType == ContentType.UNKNOWN) {
        	logInfo.addError(MessageFormat.format(ConstField.ILLEGALPACKAGETYPE, notification.getContentType()));
        	return false;
        }
        
        return true;
    }
    
    /**
     * 搜索全局时间表,确定触发告警事件时的时间是否在告警间隔内
     * @param notification 一次告警事件的实体
     * @param currentTime 触发告警事件时的时间戳
     * @return 当前时间仍在告警间隔内返回false
     */
    protected boolean checkInterval(Notification notification, long currentTime) {
        long lastTime = -1;
        long interval = -1;
        
        if(!StringUtils.isBlank(notification.getInterval())) {
            interval = Long.parseLong(notification.getInterval()) * 60 * 1000L;
        }
        
        /* 根据包名、包类型以及当前告警的等级确定唯一的告警项 */
        if(!StringUtils.isBlank(notification.getPackageName()) && !StringUtils.isBlank(notification.getState())) {
            StringBuilder builder = new StringBuilder().append(notification.getPackageName())
                    .append(notification.getState()).append(notification.getContentType()).append(StringUtils.defaultString(notification.getExtend().get(FOCUSCONTACTFLAG),""));
            uniqueID = NotifyUtils.md5Hex(builder.toString(), ConstField.UTF8);
        }
        
        if(timeMaps.containsKey(uniqueID)) {
            lastTime = timeMaps.get(uniqueID);      /* 存在该告警项时，从Map中读取上次发送告警的时间 */
        }
        
        if(lastTime != -1 && (currentTime - lastTime < interval)) {
            logInfo.addError(MessageFormat.format(ConstField.DISABLENOTIFICATION, 
                    MessageFormat.format(ConstField.TOOFREQUENTACTION, 
                            currentTime - lastTime, interval)));
            return false;
        }
        
        return true;
    }

    public void setLogInfo(LogInfo logInfo) {
        this.logInfo = logInfo;
    }
}
